home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / cdrecord-1.8.1 / lib / movebytes.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-12  |  2.8 KB  |  124 lines

  1. /* @(#)movebytes.c    1.11 00/04/12 Copyright 1985 J. Schilling */
  2. /*
  3.  *    move data
  4.  *
  5.  *    Copyright (c) 1985 J. Schilling
  6.  */
  7. /*
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2, or (at your option)
  11.  * any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; see the file COPYING.  If not, write to
  20.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23. #include <standard.h>
  24. #include <align.h>
  25.  
  26. #define    DO8(a)    a;a;a;a;a;a;a;a;
  27.  
  28. char *movebytes(fromv, tov, cnt)
  29.     const void    *fromv;
  30.     void        *tov;
  31.     int        cnt;
  32. {
  33.     register const char    *from    = fromv;
  34.     register char        *to    = tov;
  35.     register int        n;
  36.  
  37.     /*
  38.      * If we change cnt to be unsigned, check for == instead of <=
  39.      */
  40.     if ((n = cnt) <= 0)
  41.         return (to);
  42.  
  43.     if (from >= to) {
  44.         /*
  45.          * source is on higher adresses than destination:
  46.          *    move bytes forwards
  47.          */
  48.         if (n >= (int)(8 * sizeof(long))) {
  49.             if (l2aligned(from, to)) {
  50.                 register const long *froml = (const long *)from;
  51.                 register long *tol = (long *)to;
  52.                 register int rem = n % (8 * sizeof (long));
  53.             
  54.                 n /= (8 * sizeof (long));
  55.                 do {
  56.                     DO8 (*tol++ = *froml++);
  57.                 } while (--n > 0);
  58.  
  59.                 from = (const char *)froml;
  60.                 to = (char *)tol;
  61.                 n = rem;
  62.             }
  63.  
  64.             if (n >= 8) {
  65.                 n -= 8;
  66.                 do {
  67.                     DO8 (*to++ = *from++);
  68.                 } while ((n -= 8) >= 0);
  69.                 n += 8;
  70.             }
  71.  
  72.             if (n > 0) do {
  73.                 *to++ = *from++;
  74.             } while (--n > 0);
  75.             return (to);
  76.         }
  77.         if (n > 0) do {
  78.             *to++ = *from++;
  79.         } while (--n > 0);
  80.         return (to);
  81.     } else {
  82.         char *ep;
  83.  
  84.         /*
  85.          * source is on lower adresses than destination:
  86.          *    move bytes backwards
  87.          */
  88.         to += n;
  89.         from += n;
  90.         ep = to;
  91.         if (n >= (int)(8 * sizeof(long))) {
  92.             if (l2aligned(from, to)) {
  93.                 register const long *froml = (const long *)from;
  94.                 register long *tol = (long *)to;
  95.                 register int rem = n % (8 * sizeof (long));
  96.  
  97.                 n /= (8 * sizeof (long));
  98.                 do {
  99.                     DO8 (*--tol = *--froml);
  100.                 } while (--n > 0);
  101.  
  102.                 from = (const char *)froml;
  103.                 to = (char *)tol;
  104.                 n = rem;
  105.             }
  106.             if (n >= 8) {
  107.                 n -= 8;
  108.                 do {
  109.                     DO8 (*--to = *--from);
  110.                 } while ((n -= 8) >= 0);
  111.                 n += 8;
  112.             }
  113.             if (n > 0) do {
  114.                 *--to = *--from;
  115.             } while (--n > 0);
  116.             return (ep);
  117.         }
  118.         if (n > 0) do {
  119.             *--to = *--from;
  120.         } while (--n > 0);
  121.         return (ep);
  122.     }
  123. }
  124.